#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

template<class T> void f(T)
{
    string line;
    getline(cin, line);
    stringstream ss(line);
    T cur;
    vector<T> arr;
    while (ss >> cur) arr.push_back(cur);
    int n; cin >> n;
    while (n--) {
        int t; cin >> t; cout << t << endl;
        for (int i = 0; i < t; i++) {
            for (int j = i; j < arr.size(); j += t) {
                int tgt = j;
                for (int k = j + t; k < arr.size(); k += t) {
                    if (arr[tgt] > arr[k]) tgt = k;
                }
                swap(arr[j], arr[tgt]);
            }
        }
        for (int i = 0; i < arr.size(); ++i) {
            if (i) cout << ',';
            cout << arr[i];
        }
        if (n) cout << endl;
    }
}

int main()
{
    int t; cin >> t; (void)getchar();
    if (t < 0 || t > 3) cout << "err";
    else if (t == 0)
        f(int());
    else if (t == 1)
        f(double());
    else if (t == 2)
        f(char());
    else if (t == 3)
        f(string());
    return 0;
}